<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button @click="isEdit=!isEdit" size="mini" round color="red" class="edit-btn" plain type="primary">
        {{isEdit?'完成':'编辑'}}
      </van-button>
    </van-cell>
    <van-grid class="my-grid" :gutter="10" :border="false">
      <van-grid-item
        :class="{active:index===active}"
        :icon="isEdit&&index!==0?'close':''"
        v-for="(item,index) in channels"
        :key="item.id"
        :text="item.name"
        @click="onMyClick(index,item.id)"
      />
    <!-- 推荐频道 -->
    </van-grid>
        <van-cell title="推荐频道" :border="false">
    </van-cell>
    <van-grid icon-size="0.5rem" class="my-grid" direction="horizontal" :gutter="10" :border="false">
      <van-grid-item @click="onRecommendChannels(item1)" icon="plus" v-for="item1 in recommendChannels" :key="item1.id" :text="item1.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChabbels } from '@/api/article'
import { Toast } from 'vant'
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage'
import { addUserChannelsItem, removeUserChannelItem } from '@/api/user'
export default {
  name: 'ChannelEdit',
  props: {
    channels: {
      type: Array, // 获取数组里面的  必须 单词
      required: true// 必须的
    },
    active: {
      type: Number, // 获取数组里面的  必须 单词
      required: true// 必须的
    }
  },
  data () {
    return {
      isEdit: false,
      allChannels: []
    }
  },

  created () {
    this.loadAllChannel()
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      // 对allChannels进行筛选
      // allChannels的每一项 是否在channels里,如果没有在channels,属于recommendChannels
      // const arr = this.allChannels.filter(item => {
      //   const flag = this.channels.some(item2 => {
      //     return item2.id === item.id
      //   })
      //   return !flag
      // })
      // return arr
      return this.allChannels.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
    }

  },
  methods: {
    onMyClick (index, id) {
      if (this.isEdit) {
        // this.isEdit===true 时是编辑状态 反之要删除
        if (index === 0) return
        console.log('删除' + index)
        if (index <= this.active) {
          this.$emit('setActive')
        }
        // this.$emit('setActive', index)
        this.channels.splice(index, 1)
        this.deleteChannelStorage(id)
      } else {
        console.log('跳转' + index)
        this.$emit('onGo', index)
      }
    },
    async deleteChannelStorage (id) {
      // 未登录
      if (!this.user) return setStorage('MY-CHANNELS', this.channels)
      // 已登录
      Toast.loading({
        message: '删除中...',
        forbidClick: true
      })
      try {
        await removeUserChannelItem(id)
        Toast.success('删除成功')
      } catch (err) {
        Toast.fail('删除失败')
      }
    },
    async loadAllChannel () {
      try {
        const res = await getAllChabbels()
        console.log(res.channels)
        this.allChannels = res.channels
      } catch (err) {
        Toast('获取所有频道失败')
      }
    },
    async onRecommendChannels (obj) {
      this.channels.push(obj)
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      if (this.user) {
        try {
          await addUserChannelsItem({
            id: obj.id,
            seq: this.channels.length
          })
          Toast.success('添加成功')
        } catch (err) {
          Toast.fail('添加失败')
        }
      } else {
        setStorage('MY-CHANNELS', this.channels)
        Toast.success('添加成功')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit{
  .edit-btn{
    width: 104px;
    height: 48px;
  }
  /deep/.my-grid{
    .van-grid-item{
      width: 160px;
      height: 86px;
      border-radius: 6px;
      .van-grid-item__content{
        background: #F4F5F6;
      }
    }
    .van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -10.5px;
      right: 5px;
    }
    .van-grid-item__icon+.van-grid-item__text {
      margin-top: 0;
    }
    .active{
      .van-grid-item__text{
        color: red;
      }
    }
  }
}
</style>
